﻿import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import PageResult from '@/store/entities/page-result';
import Ajax from '@/lib/ajax';
import DictionaryCategory from '@/store/entities/administration/dictionary/dictionary-category';

interface DictionaryCategoryState extends ListState<DictionaryCategory>{
    dictionaryCategoryForEdit:any,
    excelFile:any,
    dictionaryCategory:any,
}

class DictionaryCategoryModule extends ListModule<DictionaryCategoryState,any,DictionaryCategory>{
    state={
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<DictionaryCategory>(),
        loading: false,
        dictionaryCategoryForEdit:{},
        excelFile:{},
        dictionaryCategory:{},
    };
    actions={
        async obtainDictionaryCategories(context:ActionContext<DictionaryCategoryState, any>,payload:any){
            context.state.loading=true;
            const response=await Ajax.post('/api/services/app/DictionaryCategory/ObtainDictionaryCategories',payload.data);
            context.state.loading=false;
            const result=response.data.result as PageResult<DictionaryCategory>;
            context.state.totalCount=result.totalCount;
            context.state.list=result.items;
        },
        async createOrUpdateDictionaryCategory(context:ActionContext<DictionaryCategoryState, any>,payload:any){
            await Ajax.post('/api/services/app/DictionaryCategory/CreateOrUpdateDictionaryCategory',payload.data);
        },
        async deleteDictionaryCategory(context:ActionContext<DictionaryCategoryState, any>,payload:any){
            await Ajax.delete('/api/services/app/DictionaryCategory/DeleteDictionaryCategory?Id='+payload.data.id);
        },
        async getDictionaryCategoryForEdit(context:ActionContext<DictionaryCategoryState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/DictionaryCategory/GetDictionaryCategoryForEdit?Id='+payload.data.id);
            context.state.dictionaryCategoryForEdit=response.data.result;
        },
        async getDictionaryCategory(context:ActionContext<DictionaryCategoryState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/DictionaryCategory/GetDictionaryCategory?Id='+payload.data.id);
            context.state.dictionaryCategory=response.data.result;
        },
        async obtainDictionaryCategoriesToExcel(context:ActionContext<DictionaryCategoryState, any>,payload:any){
            const response=await Ajax.post('/api/services/app/DictionaryCategory/ObtainDictionaryCategoriesToExcel',payload.data);
            context.state.excelFile=response.data.result;
        },
    };
    mutations = {
        setCurrentPage(state: DictionaryCategoryState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: DictionaryCategoryState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}
const dictionaryCategoryModule=new DictionaryCategoryModule();
export default dictionaryCategoryModule;
